package cn.yunzhisheng.vui.assistant.tv.view;

import java.util.Timer;
import java.util.TimerTask;

import cn.yunzhisheng.vui.assistant.tv.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class GloblFloatView extends FloatView {
    private static final String TAG = "MicrophoneFloatView";
    private View view;
    private ImageView mVoiceBg;
    private ImageView mVoiceLevel;
    private ImageView mVoiceRing;
    private TextView mStateText;
    private TextView mShortText;
    private ImageView mRatoteImage;
    private Animation operatingAnim;
    private LinearInterpolator lin;
    private Animation mRecognitionScaleAnim;
    private Animation mTranslateDownUpAnim;
    private Animation mTranslateDownInAnim;
    private Animation mTranslateDownIn2Anim;
    private Animation mTranslateRightAnim;
    private Animation mAlaphaInAnim;
    private Animation mAlaphaHalfAnim;
    private Animation mAlaphaAllAnim;
    private Animation mAlaphaOutAnim;
    private LinearLayout voice_start;
    private LinearLayout pop_start;
    private String mProtocal;
    private int mVolume;

    private Handler handler = new Handler();

    private Timer timer;

    public GloblFloatView(Context context) {
        super(context);
        init();
        initViewStyle();
    }

    private void initViewStyle() {
        mWindowParams.type = android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        mWindowParams.format = PixelFormat.RGBA_8888;
        mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        mWindowParams.gravity = Gravity.CENTER;
        mWindowParams.width = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
        mWindowParams.height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
    }

    public void setProtocal(String protocal) {
        mProtocal = protocal;
    }

    public void updateVoiceView(VoiceMode mode) {
        Log.d(TAG, "global record sound update view " + mode);
        switch (mode) {
        case MODE_RECORDING:
            setVisibility(View.VISIBLE);
            mShortText.setVisibility(View.GONE);
            mRatoteImage.clearAnimation();
            mRatoteImage.setVisibility(View.GONE);
            mStateText.clearAnimation();
            // timer.scheduleAtFixedRate(new RefreshTask(), 0, 100);
            voice_start.startAnimation(mAlaphaAllAnim);
            mVoiceRing.setVisibility(View.VISIBLE);
            mVoiceBg.setVisibility(View.VISIBLE);
            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_release);
            mVoiceBg.setImageResource(R.drawable.ic_voice_speaking);
            mVoiceLevel.setVisibility(View.VISIBLE);
            break;
        case MODE_RECOGNISING:
            setVisibility(View.VISIBLE);
            mShortText.setVisibility(View.GONE);
            pop_start.startAnimation(mAlaphaOutAnim);
            pop_start.setVisibility(View.INVISIBLE);
            mStateText.setVisibility(View.VISIBLE);
            mRatoteImage.setVisibility(View.VISIBLE);
            mRatoteImage.startAnimation(operatingAnim);
            mVoiceBg.setImageResource(R.drawable.ic_voice_prasing);
            mVoiceRing.setVisibility(View.GONE);

            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_recognising);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_UNRECOGNISED:
            setVisibility(View.VISIBLE);
            mStateText.setVisibility(View.VISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default);
            setRatoteImageGone();
            mStateText.setText(R.string.voice_unrecognised);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_NO_VOICE:
            setVisibility(View.VISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default);
            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_no);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_RECORDINGINIT:
            setVisibility(View.VISIBLE);
            mShortText.setVisibility(View.GONE);
            view.startAnimation(mAlaphaInAnim);
            voice_start.startAnimation(mRecognitionScaleAnim);
            voice_start.startAnimation(mAlaphaHalfAnim);
            pop_start.setVisibility(View.VISIBLE);
            pop_start.startAnimation(mTranslateDownUpAnim);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default);
            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_recording_init);
            mStateText.startAnimation(mTranslateDownInAnim);

            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_UNAVILIABLE:
            setEnabled(false);
            break;
        case MODE_DEFAULT:
            setEnabled(true);
            mStateText.setVisibility(View.VISIBLE);
            pop_start.setVisibility(View.INVISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default_current);
            mVoiceLevel.setVisibility(View.GONE);
            setRatoteImageGone();
            break;
        case MODE_PROTOCAL:
            setEnabled(true);
            mStateText.setVisibility(View.VISIBLE);
            mShortText.setVisibility(View.GONE);
            // mStateText.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
            mStateText.setTextColor(getResources().getColor(R.color.global_text));
            mStateText.setText(mProtocal);
            // mStateText.startAnimation(mTranslateRightAnim);
            pop_start.setVisibility(View.INVISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setVisibility(View.VISIBLE);
            mVoiceBg.setImageResource(R.drawable.ic_voice_default_current);
            mRatoteImage.clearAnimation();
            mRatoteImage.setVisibility(View.GONE);
            mVoiceLevel.setVisibility(View.GONE);
            break;
        case MODE_RECORDINGSHORT:
            setEnabled(true);
            voice_start.clearAnimation();
            pop_start.clearAnimation();
            mStateText.clearAnimation();
            pop_start.setVisibility(View.INVISIBLE);
            mVoiceRing.setVisibility(View.INVISIBLE);
            mVoiceBg.setVisibility(View.INVISIBLE);
            mRatoteImage.clearAnimation();
            mRatoteImage.setVisibility(View.GONE);
            mVoiceLevel.setVisibility(View.GONE);
            mStateText.setVisibility(View.VISIBLE);
            mStateText.setText(R.string.voice_too_short_title);
            mShortText.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hide() {
        voice_start.startAnimation(mAlaphaOutAnim);
        setRatoteImageGone();
        if (timer != null) {
            timer.cancel();
        }
        super.hide();
    }

    @Override
    public void show() {
        if (timer == null) {
            timer = new Timer();
        }
        super.show();
    }

    private void setRatoteImageGone() {
        mStateText.setTextColor(getResources().getColor(R.color.global_record));
        mShortText.setVisibility(View.GONE);
        mRatoteImage.clearAnimation();
        mRatoteImage.setVisibility(View.GONE);
    }

    public void updateVolume(int volume) {
        mVolume = volume;
        /*
         * if (mVolume > 10) { mVoiceRing.setVisibility(View.VISIBLE);
         * mVoiceLevel.setVisibility(View.VISIBLE); setRatoteImageGone(); }
         */

        if (mVolume < 10) {
            setVoiceLevel(1);
        } else if (mVolume < 20) {
            setVoiceLevel(2);
        } else if (mVolume < 30) {
            setVoiceLevel(3);
        } else if (mVolume < 40) {
            setVoiceLevel(4);
        } else if (mVolume < 50) {
            setVoiceLevel(5);
        } else if (mVolume < 60) {
            setVoiceLevel(6);
        } else if (mVolume < 70) {
            setVoiceLevel(7);
        } else if (mVolume < 80) {
            setVoiceLevel(8);
        } else if (mVolume < 90) {
            setVoiceLevel(9);
        } else {
            setVoiceLevel(10);
        }
    }

    public void setVoiceLevel(int level) {
        mVoiceLevel.getDrawable().setLevel(level);
    }

    private void init() {
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        view = LayoutInflater.from(getContext()).inflate(R.layout.global_float_view, this);

        voice_start = (LinearLayout) view.findViewById(R.id.voice_start);
        pop_start = (LinearLayout) view.findViewById(R.id.pop_start);

        mVoiceBg = (ImageView) view.findViewById(R.id.voice_bg);
        mVoiceLevel = (ImageView) view.findViewById(R.id.voice_level);
        mVoiceRing = (ImageView) view.findViewById(R.id.voice_ring);
        mStateText = (TextView) view.findViewById(R.id.state_text);
        mShortText = (TextView) view.findViewById(R.id.short_text);

        mRatoteImage = (ImageView) view.findViewById(R.id.rotate_animation);

        lin = new LinearInterpolator();
        operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_sacnner);
        operatingAnim.setInterpolator(lin);

        /** ---Dialog 进入Scale动画---modify by WLP at 2013-12-04--- */
        mRecognitionScaleAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_scale_in);
        mTranslateDownUpAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_downtoup_in);
        mTranslateDownInAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_down_in);
        mTranslateDownIn2Anim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_down_in2);
        mTranslateRightAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_right_in);
        mAlaphaInAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_alpha_in);
        mAlaphaHalfAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_alpha_half);
        mAlaphaAllAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_alpha_all);
        mAlaphaOutAnim = AnimationUtils.loadAnimation(getContext(), R.anim.global_voice_alpha_out);
        /** ---Dialog 进入Scale动画---modify by WLP at 2013-12-04--- */
    }

    class RefreshTask extends TimerTask {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (mVolume > 10) {
                        timer.cancel();
                        voice_start.startAnimation(mAlaphaAllAnim);
                        mVoiceRing.setVisibility(View.VISIBLE);
                        mVoiceBg.setVisibility(View.VISIBLE);
                        mStateText.setText(R.string.voice_release);
                        mVoiceBg.setImageResource(R.drawable.ic_voice_speaking);
                        mVoiceLevel.setVisibility(View.VISIBLE);
                    }
                }
            });
        }
    }
}
